import {getETSCBalance, getETHBalance} from "../etherjs/account";
import store from "@/store/index";
import axios from "axios";

export function synBalance() {
    // 获取当前walletActive
    synBalanceOnce();
    setInterval(() => {
        synBalanceOnce();
    }, 15000);
}

export function synBalanceOnce() {
    // 获取当前walletActive
    let address = localStorage.getItem("walletActive");
    if (address) {
        try {
            getETHBalance(address).then((ETH) => {
                store.dispatch('aBalance', {ETH})
            });
            getETSCBalance(address).then((ETSC) => {
                store.dispatch('aBalance', {ETSC})
            });
            getETSCBalance("0x80f1b85d297e2ad3f76c99f1059f13670c13243b").then((SICBO) => {
                store.dispatch('aBalance', {SICBO})
            });
        } catch (err) {
            console.log(err)
        }

    }
}

export function synCurrency() {
    try {
        let ws = new WebSocket("ws://www.winerbit.com:20021");

        ws.onopen = function () {
            console.log("Connection open ...");
            ws.send('{"token":"ETSC","identifier":"","params":{"method":"DBV_MARKET_MAIN"}}');
        };

        ws.onmessage = function (evt) {
            try {
                let obj = JSON.parse(evt.data);
                let data = obj.Data.Data.frames;
                let vuex = {};
                for (let i in data) {
                    if (data[i].areaname == "CTC") {
                        vuex[data[i].typecode] = data[i].today_price_new;
                    }
                }
                store.dispatch('aCurrency', vuex);
            } catch (e) {
                console.log(e);
            }

        };

        ws.onclose = function () {
            console.log("Connection closed.");
        };
    } catch (e) {
        console.log(e);
    }
}

export async function synExchange() {
    try {
        let USD = await axios.get('https://api.coinbase.com/v2/prices/BTC-USD/spot').then((response) => {
            return response.data.data.amount;
        });
        let CNY = await axios.get('https://api.coinbase.com/v2/prices/BTC-CNY/spot').then((response) => {
            return response.data.data.amount;
        });
        let rate = CNY / USD;
        let type = localStorage.getItem("currency") ? localStorage.getItem("currency") : "USD";

        store.dispatch('aExchange', {rate, type});
    } catch (e) {
        console.log(e);
    }

}

export function synWallet() {
    //1.active地址
    let addressActive = localStorage.getItem("walletActive");
    if (addressActive) {
        let obj = JSON.parse(localStorage.getItem(addressActive));
        let addressObj = {
            address: addressActive,
            keystore: obj.keystore,
            backUp: obj.backUp,
            mnemonic: obj.mnemonic ? obj.mnemonic : "",
            name: obj.name ? obj.name : "",
            pKey: obj.pKey,
            tip: obj.tip ? obj.tip : "",
            avatar: obj.avatar,
            index: obj.index
        };
        store.dispatch('wallet/aWalletActive', addressObj)
    }

    //2.create地址
    let addressCreate = localStorage.getItem("walletCreate");
    if (addressCreate) {
        let obj = JSON.parse(localStorage.getItem(addressCreate));
        let addressObj = {
            address: addressCreate,
            keystore: obj.keystore,
            backUp: obj.backUp,
            mnemonic: obj.mnemonic ? obj.mnemonic : "",
            name: obj.name ? obj.name : "",
            pKey: obj.pKey,
            tip: obj.tip ? obj.tip : "",
            avatar: obj.avatar,
        };
        store.dispatch('wallet/aWalletCreate', addressObj)
    }
}

